/********************************************************************************
 * Copyright(c) 2020-2025 BST All rights reserved.
 * - Filename
 * - Author
 * - Version V1.0.0
 * - Date    2024/08/28
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */

#pragma once

#include "Lidar.hpp"
#include "inno_lidar_api.h"
#include "TimeUtil.h"
#include <condition_variable>
#include <deque>
#include <mutex>

namespace innolidar
{

  class InnoLidarAdapter : public lidar::Lidar
  {
  public:
    InnoLidarAdapter(const uint32_t &id, const std::string&ip, const uint16_t &msop_port,
                     const uint16_t &difop_port);
    ~InnoLidarAdapter();
    void start(
        std::function<void(std::shared_ptr<lpc::LidarProtoPointCloud> &&)>) override;
    void stop() override;

  protected:
    const uint32_t _id;
    const std::string _ip;
    const uint16_t _msopPort;
    const uint16_t _difopPort;

  private:
    bool _isExit{true};
    int _innoLidarHandl{-1};
    int64_t _isNewFrame{0};
    uint64_t _pointCounter{0};
    TimeUtil::FpsCalc _fps{};
    std::shared_ptr<std::thread> _packageThread{nullptr};
    std::shared_ptr<lpc::LidarProtoPointCloud> _oneFrame{nullptr};
    std::mutex _mtx;
    std::condition_variable _condition;
    std::deque<std::shared_ptr<lpc::LidarProtoPointCloud>> _lidarProtoPointCloudDeque{};
    int innoLidarPointCloudCB(int handl, const InnoDataPacket &pkt);
    void
    processLoop(const std::function<void(std::shared_ptr<lpc::LidarProtoPointCloud> &&)>
                    &callback);
  };
} // namespace lslidar